import pandas as pd

def get_manufacturer_data_optimized(df, manufacturer, year):
    """
    获取厂商数据（优化版）
    参数: df - 预加载的销售数据DataFrame
    """
    # 筛选指定年份
    if year != 'all':
        df = df[df['年份'] == int(year)]

    # 按厂商分组聚合
    grouped = df.groupby('厂商')['销量'].sum().reset_index()

    # 计算总销量
    total_sales = grouped['销量'].sum()

    # 处理厂商筛选
    if manufacturer != 'all':
        grouped = grouped[grouped['厂商'] == manufacturer]

    # 计算市场份额
    grouped['市场份额'] = (grouped['销量'] / total_sales * 100).round(2)

    # 按销量排序
    grouped = grouped.sort_values('销量', ascending=False)

    # 转换为列表格式
    result = grouped.to_dict('records')

    return {
        'success': True,
        'data': result,
        'total_sales': total_sales
    }